<?php
    class WZ_Logger 
    {
        private static $logger = array();
        private static $priorities = array();
        private static $enabled = false;
        
        public static function setLogger($logger) 
        {
            self::$logger = array();
            self::$logger[] = $logger;
            self::$enabled = true;
        }

        public static function resetLoggers() 
        {
            self::$logger = array();
        }
        
        public static function addLogger($logger, $reset = false) 
        {
            if($reset) {
                self::$logger = array();
            }
            self::$logger[] = $logger;
            self::$enabled = true;
        }
        
        public static function getLogger() 
        {
            return self::$logger;
        }
        
        public static function setPriorities($prios) 
        {
            self::$priorities = $prios;
        }
        
        public static function initDummy() 
        {
            self::$enabled = false;
        }

        public static function disable() 
        {
            self::$enabled = false;
        }

        public static function enable() 
        {
            self::$enabled = true;
        }
        
        public static function log ($message, $code = \Zend_Log::INFO) 
        {
            if(!self::$enabled) {
                return;
            }
            
            if(in_array($code,self::$priorities)) {

                $backtrace = debug_backtrace();
                $call = $backtrace[2];
                $call["line"] = $backtrace[1]["line"];

                if(is_object($message) || is_array($message)) {
                    if($message instanceof Exception) {
                        $message = $call["class"] . $call["type"] . $call["function"] . "() [" . $call["line"] . "]: [Exception] with message: " . $message->getMessage()
                            ."\n"
                            ."In file: "
                            .$message->getFile()
                            . " on line "
                            .$message->getLine()
                            ."\n"
                            .$message->getTraceAsString();
                    }
                    else {
                        $message = print_r($message,true);
                    }
                } else {
                    $message = $call["class"] . $call["type"] . $call["function"] . "() [" . $call["line"] . "]: " . $message;
                }

                foreach (self::$logger as $logger) {
                    $logger->log($message,$code);
                }
            }
        }
        
        
        public static function emergency($m, $l = null) 
        {
            self::log($m, \Zend_Log::EMERG);
        }
        
        public static function emerg($m, $l = null) 
        {
            self::log($m, \Zend_Log::EMERG);
        }
        
        public static function critical($m, $l = null) 
        {
            self::log($m, \Zend_Log::CRIT);
        }
        
        public static function crit($m, $l = null) 
        {
            self::log($m, \Zend_Log::CRIT);
        }
        
        public static function error($m, $l = null) 
        {
            self::log($m, \Zend_Log::ERR);
        }
        
        public static function err($m, $l = null) 
        {
            self::log($m, \Zend_Log::ERR);
        }
        
        public static function alert($m, $l = null) 
        {
            self::log($m, \Zend_Log::ALERT);
        }
        
        public static function warning($m, $l = null) 
        {
            self::log($m,\Zend_Log::WARN);
        }
        
        public static function warn($m, $l = null) 
        {
            self::log($m, \Zend_Log::WARN);
        }
        
        public static function notice($m, $l = null) 
        {
            self::log($m, \Zend_Log::NOTICE);
        }
        
        public static function info($m, $l = null) 
        {
            self::log($m, \Zend_Log::INFO);
        }
        
        public static function debug($m, $l = null) 
        {
            self::log($m, \Zend_Log::DEBUG);
        }
    }
